package note;
//Description:拷贝 以及一些方法
import java.util.Arrays;

public class Note6 {
    public static void main(String[] args) {
        int[] array = {12,15,16,5,6};
        int[] a = new int[5];
        Arrays.fill(a,2,6,15);//填充数组      [2,6)填充15
        //Arrays.sort(array);
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
        int[] hh = Arrays.copyOf(array,array.length);//也可以扩大数组给array.length * 2
        //浅拷贝(改变（拷贝）地址)
        System.out.println(Arrays.toString(hh));
        int[] hhh = Arrays.copyOfRange(array,1,3);//copy局部数组  边界值不能超过原数组
        //浅拷贝
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);//native修饰的已经被实现了 特点快
        //浅拷贝
        int[] copy1 = array.clone();//可以直接copy（.clone产生一个副本）浅拷贝
        System.out.println(Arrays.toString(copy1));
        int[][] arrayd = {{1,2,3},{2,5,6}};
        int[][] arrayd1 = new int[2][3];
        for (int i = 0; i < arrayd.length; i++) {
            for (int j = 0; j < arrayd[i].length; j++) {
                System.out.print(arrayd[i][j] + " ");
            }
            System.out.println();
        }
        System.out.println("========");
        for (int[] ret:arrayd) {
            for (int x:ret) {
                System.out.println(x + " ");
            }
        }
        System.out.println("======");
        System.out.println(Arrays.deepToString(arrayd));
        int[][] arraydd = new int[2][];
        arraydd[0] = new int[3];
        arraydd[2] = new int[2];//不规则二维数组
    }
    //冒泡排序
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length - 1; i++) {//i 代表趟数
            boolean n = false;//判断有无交换
            for (int j = 0; j < array.length - 1 - i; j++) {//j代表每一次需要交换的次数
                if(array[j] > array[j + 1]){
                    int t = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = t;
                    n = true;
                }
            }
            if(n = false){
                return ;
            }
        }
    }
    //逆置数组
    public static void recerse(int[] array){
        int i = 0;
        int j = array.length - 1;
        while(i < j){
            int t = array[j];
            array[j] = array[i];
            array[i] = t;
            i++;
            j--;
        }
    }
    //偶数放在奇数前面
    public static void so(int[] array){
        int i = 0;
        int j = array.length;
        while(i < j){
            while(array[i] % 2 == 0 && i < j){
                i ++;
            }//到现在i下标一定是奇数
            while(array[j] % 2 == 1 && i < j){
                j --;
            }//到现在j一定是偶数
            int t = array[j];
            array[j] = array[i];
            array[i] = t;
        }
    }
    //拷贝数组  深拷贝（改变（拷贝）对象）
    public static int[] copy(int[] array){
        int[] h = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            h[i] = array[i];
        }
        return h;
    }
}
